import React from "react";
import Relay from 'react-relay';

// 自有组件
import Title from './components/VIew_Title';
import Head from './components/Logic_Head';
import CardMain from './components/Logic_CardMain';

// antd
import {message,Spin} from 'antd';

// mutaion
import orderTradeTree from './mutation/orderTradeTree';

import './css/main.css';

export default class UpDownStreamShow extends React.Component {
    constructor(props) {
        super(props);
        //TD20170605080000019631
        this.state = {
            orderId:"",  // 订单号ll
            subOrders:[],   // ;list 数据
            headData:[],    //head 部分数据
        };
        this.httpRequest = this.httpRequest.bind(this);
    }

    componentWillMount(){
        if(this.props.location.state && this.props.location.state.oid){
            this.oid = this.props.location.state.oid;
            // 更新数据
            this.httpRequest();
        }else{
            message.info("请按规则进入此页面");
        }
    }

    // 更新数据
    httpRequest(){
        const oid = this.oid;

        if(!oid){ message.info("请从正常入口进入此页面"); return; }
        Relay.Store.commitUpdate(
            new orderTradeTree({
                orderId:oid,
                enId:sessionStorage.getItem("enterpriseId")
            }),{
                onFailure:(err)=>{
                    message.error("服务器错误，请联系系统管理员");
                },
                onSuccess:(res)=>{
                    const d = res.orderTradeTree.orderTradeTree;
                    if(d.errorCode == "0"){
                        this.setState({
                            headData: d.order ? JSON.parse(JSON.stringify(d.order)) : {},
                            subOrders: d.subOrders ? JSON.parse(JSON.stringify(d.subOrders)) : {}
                        })
                    }else{
                        message.error(d.value);
                    }
                    this.setState({isLoading:false})
                }
            }
        )
    }

    //asd
    render() {
        const {isLoading,subOrders,headData} = this.state;
        return(
            <Spin
                spinning={isLoading}
            >
                <div className="order_upDownStream_show" style={isLoading ? {visibility:"hidden"} : null}>
                    <header>
                        <Title
                            color="red"
                            title="销售"
                        />
                        <Head
                            data={headData}
                        />
                    </header>

                    <section>
                        <Title
                            color="main"
                            title="采购"
                        />
                        <CardMain
                            data={subOrders}
                        />
                    </section>
                </div>
            </Spin>
        )
    }
}